<template>
  <div class="app-container">
    <el-card>
      <el-form
        :model="queryParams"
        ref="queryRef"
        :inline="true"
        v-show="showSearch"
      >
        <el-form-item label="老人姓名">
          <el-input
            v-model="queryParams.applyName"
            placeholder="请输入老人姓名"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="告警类型">
          <el-select
            v-model="queryParams.alertType"
            clearable
            placeholder="请选择告警类型"
            style="width: 240px"
          >
            <el-option
              v-for="dict in alarm_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="处理状态">
          <el-select
            v-model="queryParams.status"
            clearable
            placeholder="请选择处理状态"
            style="width: 240px"
          >
            <el-option
              v-for="dict in processing_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="告警时间" style="font-weight: 700">
          <el-date-picker
            v-model="queryParams.beginTime"
            type="date"
            placeholder="选择开始时间"
            value-format="YYYY-MM-DD"
          />
          <span style="margin-left: 5px">~</span>
          <span style="margin-left: 5px">
            <el-date-picker
              v-model="queryParams.endTime"
              type="date"
              placeholder="选择结束日期"
              value-format="YYYY-MM-DD"
            />
          </span>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <right-toolbar
          v-model:showSearch="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>

      <el-table
        v-loading="loading"
        :data="postList"
        stripe
        border
        max-height="500"
      >
        <el-table-column label="老人姓名" align="center" prop="applyName" />
        <el-table-column label="联系电话" align="center" prop="telephone" />
        <el-table-column
          label="告警时间"
          align="center"
          prop="alertTime"
          width="200"
        />
        <el-table-column label="告警类型" align="center">
          <template #default="scope">
            <span>
              {{
                alarm_type.find((item) => item.value == scope.row.alertType)
                  ? alarm_type.find((item) => item.value == scope.row.alertType)
                      .label
                  : ""
              }}
            </span>
          </template>
        </el-table-column>
        <el-table-column label="告警信息" align="center" prop="alertInfo" />

        <el-table-column label="告警终端" align="center" prop="alertTerminal">
        </el-table-column>
        <el-table-column
          label="告警地址"
          align="center"
          prop="alertAddress"
          width="300"
        />
        <el-table-column label="处理状态" align="center">
          <template #default="scope">
            <span>
              {{
                processing_status.find((item) => item.value == scope.row.status)
                  ? processing_status.find(
                      (item) => item.value == scope.row.status
                    ).label
                  : ""
              }}
            </span>
          </template>
        </el-table-column>
        <el-table-column
          label="处理时间"
          align="center"
          prop="handleTime"
          width="180"
        />
        <el-table-column label="处理人员" align="center" prop="handleName" />

        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template #default="scope">
            <el-button
              link
              type="primary"
              v-if="scope.row.status == '0'"
              @click="handleInfo(scope.row)"
              >处理</el-button
            >
            <el-button
              link
              type="primary"
              v-if="scope.row.status == '1'"
              @click="handleInfo(scope.row)"
              >查看</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.page"
        v-model:limit="queryParams.limit"
        @pagination="getList"
      />
    </el-card>

    <!-- 添加或修改岗位对话框 -->
  </div>
</template>

<script setup name="apply">
const { proxy } = getCurrentInstance();
const { sys_normal_disable, sys_user_sex, alarm_type, processing_status } =
  proxy.useDict(
    "sys_normal_disable",
    "sys_user_sex",
    "alarm_type",
    "processing_status"
  );

import { debounce } from "@/utils/debounce.js";
import {
  getAlertlist,
  getElderIntelligentMonitorNumList,
} from "@/api/safehome/index";
const showSearch = ref(true);
const loading = ref(true);
const postList = ref([]);
const open = ref(false);
const router = useRouter();

const total = ref(0);

const data = reactive({
  form: {},
  queryParams: {
    page: 1,
    limit: 10,
    status: undefined,
    applyName: undefined,
    beginTime: undefined,
    endTime: undefined,
    alertType: "",
  },
  rules: {},
});

const { queryParams, form, rules } = toRefs(data);

/** 提交按钮 */

function cancel() {
  open.value = false;
  reset();
}

/** 查询岗位列表 */
function getList() {
  if (
    new Date(queryParams.value.beginTime) > new Date(queryParams.value.endTime)
  ) {
    proxy.$modal.msgError("开始时间不能大于结束时间");
    return false;
  }
  loading.value = true;
  getAlertlist(queryParams.value).then((response) => {
    postList.value = response.alertList.list;
    total.value = response.alertList.totalCount;
    loading.value = false;
  });
}
function handleInfo(data) {
  console.log("2222222222222", data);
  router.push({
    name: "dispose",
    params: {
      id: data.id,
      homeBedApplyId: data.homeBedApplyId,
      alertId: data.alertId,
      tableName: data.tableName,
      imei: data.imei,
    },
  });
}
function handleQuery() {
  getList();
}
function resetQuery() {
  queryParams.value = {
    page: 1,
    limit: 10,
    status: undefined,
    applyName: undefined,
    beginTime: undefined,
    endTime: undefined,
    alertType: undefined,
  };
  getList();
}

getList();
</script>
